home *** CD-ROM | disk | FTP | other *** search
/ Saar AMOK 2 / Saar AMOK II - Oktober 1994 (1994)(Kreativ Marketing)(DE)[!][I-7598].iso / disks / 651_700 / 652 / xpkdisk / source.lha / magic.c < prev    next >
C/C++ Source or Header  |  1993-11-08  |  5KB  |  224 lines

  1. /*-
  2.  * MAGIC.C
  3.  *
  4.  * The xpkdisk.device code that looks into DOS data structures.
  5.  *
  6.  * $Id: magic.c,v 1.3 1993/11/08 13:11:15 Rhialto Rel $
  7.  * $Log: magic.c,v $
  8.  * Revision 1.3  1993/11/08  13:11:15  Rhialto
  9.  * Add RCS tags.
  10.  *
  11.  *
  12.  * This code is (C) Copyright 1993 by Olaf Seibert. All rights reserved.
  13.  * May not be used or copied without a licence.
  14. -*/
  15.  
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include "xpkdisk.h"
  20. #ifndef LIBRARIES_FILEHANDLER_H
  21. #include <libraries/filehandler.h>
  22. #endif
  23.  
  24. /*#undef DEBUG      /**/
  25. #ifdef DEBUG
  26. #   include "syslog.h"
  27. #else
  28. #   define    debug(x)
  29. #endif
  30.  
  31. extern struct DosLibrary *DOSBase;
  32.  
  33. int
  34. FileNamesMatch(char *path, char *name)
  35. {
  36.     char       *s;
  37.  
  38.     if (s = strchr(path, ':'))
  39.     path = s + 1;
  40.     if (s = strrchr(path, '/'))
  41.     path = s + 1;
  42.     return !strcmp(path, name);
  43. }
  44.  
  45. /*
  46.  * Try to get the DosEnvec values from the mountlist. This is quite a
  47.  * work!
  48.  * We must be reasonable sure that the dn_Startup fields both refer
  49.  * to a FileSysStarupMsg. They must both be pointers (and not some
  50.  * small integral values, as used to distinguish CON and RAW, for
  51.  * example).
  52.  */
  53.  
  54. struct DosEnvec *
  55. IsOurDevicenode(UNIT *unit, struct DeviceNode *dn)
  56. {
  57.     struct FileSysStartupMsg *dnmsg;
  58.  
  59.     if (dn->dn_Type != DLT_DEVICE)
  60.     return NULL;
  61.  
  62. #ifdef DEBUG
  63.     {
  64.     char *s = BADDR(dn->dn_Name);
  65.     debug(("%.*s:\n", *s, s + 1));
  66.     }
  67. #endif
  68.     dnmsg = BADDR(dn->dn_Startup);
  69.     debug(("Startup: %x, '%.20s' %d\n", dnmsg,
  70.     (char *)BADDR(dnmsg->fssm_Device) + 1,
  71.     dnmsg->fssm_Unit ));
  72.     if (dnmsg &&
  73.     (long)dnmsg > 4*100 &&
  74.     dnmsg->fssm_Device &&
  75.     FileNamesMatch((char *)BADDR(dnmsg->fssm_Device) + 1, DevName) &&
  76.     dnmsg->fssm_Unit == unit->xu_UnitNr
  77.     ) {
  78.     long           *dnenv = BADDR(dnmsg->fssm_Environ);
  79.  
  80.     /* actually struct DosEnvec */
  81.     debug(("Environ: %x\n", dnenv));
  82.  
  83.     if (dnenv && dnenv[0] >= DE_DOSTYPE) {
  84.         debug(("Found our mount information at %lx, %lx!\n", dn, dnenv));
  85.         return (struct DosEnvec *)dnenv;
  86.     }
  87.     }
  88.     return NULL;
  89. }
  90.  
  91. void
  92. MagicInitDevicenode(UNIT *unit)
  93. {
  94.     struct RootNode *rn = (struct RootNode *) DOSBase->dl_Root;
  95.     struct DosInfo *DosInfo = (struct DosInfo *) BADDR(rn->rn_Info);
  96.     struct DeviceNode *dn = BADDR(DosInfo->di_DevInfo);
  97.  
  98.     debug(("traverse_bcpl: %x\n", dn));
  99.     while (dn) {
  100.     struct DosEnvec *de;
  101.  
  102.     if (de = IsOurDevicenode(unit, dn)) {
  103.         unit->xu_NumTracks = de->de_Surfaces * (de->de_HighCyl + 1);
  104.         unit->xu_TrackLen = 4 * de->de_SizeBlock * de->de_BlocksPerTrack;
  105.         debug(("numtracks %d tracklen %d\n",
  106.            unit->xu_NumTracks, unit->xu_TrackLen));
  107. #if 0
  108.         /* Hackery: */
  109.         debug(("tablesize %d CONTROL=%d\n", de->de_TableSize, DE_CONTROL));
  110.         if (de->de_TableSize >= DE_CONTROL) {
  111.         debug(("string %x: %.20s\n", de->de_Control, de->de_Control));
  112.         }
  113. #endif
  114.         return;
  115.     }
  116.  
  117.     dn = BADDR(dn->dn_Next);
  118.     debug(("traverse_bcpl: next is %x\n", dn));
  119.     }
  120. }
  121.  
  122. #define ON(d,s)     ((d) |= (s))
  123. #define OFF(d,s)    ((d) &= ~(s))
  124.  
  125. ulong
  126. Flag(char *opt, char *name, ulong mask, ulong previous)
  127. {
  128.     char *match;
  129.  
  130.     if (match = strstr(opt, name)) {
  131.     int o = 1;
  132.     int l = strlen(name);
  133.  
  134.     debug(("Flag: %s\n", match));
  135.     if (match[l] == '=')
  136.         o = strtol(match + l + 1, NULL, 0);
  137.     if (o)
  138.         previous |= mask;
  139.     else
  140.         previous &= ~mask;
  141.     }
  142.     return previous;
  143. }
  144.  
  145. void
  146. ParseOptions(UNIT *unit, char *opt)
  147. {
  148.     ulong        f;
  149.     char       *value;
  150.  
  151.     debug(("ParseOptions: %s\n", opt));
  152.  
  153.     if (value = strstr(opt, S_XPKPackMethod "=")) {
  154. #define SZ sizeof(unit->xu_XPKPackMethod)
  155.     int        len;
  156.  
  157.     value += sizeof(S_XPKPackMethod);
  158.     len = strcspn(value, " \t\n,;=");
  159.     len = min(len, SZ-1);
  160.     debug(("Match "S_XPKPackMethod"='%s', len %d\n", value, len));
  161.     strncpy(unit->xu_XPKPackMethod, value, len);
  162.     unit->xu_XPKPackMethod[len] = '\0';
  163.     debug(("PackMethod now %s\n", unit->xu_XPKPackMethod));
  164.     } else
  165.     debug(("Don't match "S_XPKPackMethod"=\n"));
  166.     if (value = strstr(opt, S_MaxCache "=")) {
  167.     unit->xu_MaxCache = strtol(value + sizeof(S_MaxCache), NULL, 0);
  168.     debug(("Match "S_MaxCache "='%d'\n", unit->xu_MaxCache));
  169.     }
  170.     if (value = strstr(opt, S_CacheTimeout "=")) {
  171.     unit->xu_CacheTimeout = strtol(value + sizeof(S_CacheTimeout), NULL, 0);
  172.     debug(("Match "S_CacheTimeout "='%d'\n", unit->xu_CacheTimeout));
  173.     }
  174.     f = unit->xu_CacheFlags;
  175.     f = Flag(opt, S_CMDUPDATE, CACHEF_CMDUPDATE, f);
  176.     f = Flag(opt, S_DELAY, CACHEF_DELAY, f);
  177.     f = Flag(opt, S_SAFEWRITE, CACHEF_SAFEWRITE, f);
  178.     f = Flag(opt, S_LICENSED, CACHEF_LICENSED, f);
  179.     unit->xu_CacheFlags = f;
  180. }
  181.  
  182. int
  183. ReadConfig(UNIT *unit, char *buf, int sz)
  184. {
  185.     BPTR        fh;
  186.  
  187.     fh = Open(buf, MODE_OLDFILE);
  188.     if (fh) {
  189.     int        len;
  190.  
  191.     len = Read(fh, buf, sz-1);
  192.     Close(fh);
  193.     buf[len] = 0;
  194.     ParseOptions(unit, buf);
  195.  
  196.     return 1;
  197.     }
  198.     return 0;
  199. }
  200.  
  201. /*
  202.  * First get the permanent settings, then overwrite with the
  203.  * temporary ones.
  204.  */
  205.  
  206. void
  207. MagicInitFile(UNIT *unit)
  208. {
  209.     char        buf[128];
  210.  
  211.     sprintf(buf, CONFIGFILE_ARC, unit->xu_UnitNr);
  212.     ReadConfig(unit, buf, sizeof(buf));
  213.     sprintf(buf, CONFIGFILE, unit->xu_UnitNr);
  214.     ReadConfig(unit, buf, sizeof(buf));
  215. }
  216.  
  217. Prototype void MagicInit(UNIT *unit);
  218. void
  219. MagicInit(UNIT *unit)
  220. {
  221.     MagicInitDevicenode(unit);
  222.     MagicInitFile(unit);
  223. }
  224.